"=================
"<C+o> jump to last time edited file
"<C+i> jump to next time edited file
"=================
"=================
let mapleader=" "
set encoding=utf-8
set fileencoding=utf-8
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
"language message zh_CN.utf-8
set relativenumber
set number
syntax enable
syntax on
set wildmenu
set hlsearch
set incsearch
set backspace=2
set cursorline
set ignorecase
set smartcase
set scrolloff=7
set tabstop=4
set expandtab
set shiftwidth=4

"keymap
noremap j h
noremap k j
noremap i k
noremap h i
noremap H I
map I 7i
map J 17j
map L 17l
map K 7k
imap jj <ESC>
vmap jj <ESC>
map s <nop>
nmap S :w<CR>
nmap ss :q!<CR>
map sjl :source %<CR>
map <leader><CR> :e! ~/.vim/vimrc<CR>
"paste
nmap cp :set paste<CR>"+p:set nopaste<CR>

"find something
nmap sf /

"open a tab
nmap <LEADER>et :tabe<CR>
nmap <LEADER>l :tabn<CR>
nmap <LEADER>j :tabp<CR>


"window split
nmap sw :split<CR>
nmap swi <C-w><up>
nmap swk <C-w><down>
nmap sv :vsplit<CR>
nmap svl <C-w><right>
nmap svj <C-w><left>


"find sth
nmap sf /


"press space to jump
nmap <LEADER><LEADER> <Esc>/<++><CR>:nohlsearch<CR>c4l


"=======pwnalias=========

inoremap <c-u> #!/usr/bin/python3<cr>from pwn import *<cr>#context.log_level = 'debug'<cr>context(arch='amd64', os='linux')<cr>#context(arch='i386', os='linux')<cr>context.terminal = ['tmux','splitw','-h']<cr><cr><++><cr><cr><cr>io = start()<cr>sl      = lambda s : io.sendline(s)<cr>sa      = lambda delim,data : io.sendafter(str(delim), str(data))<cr>sla     = lambda delim,data : io.sendlineafter(str(delim), str(data))<cr>sn      = lambda s : io.send(s)<cr>rc      = lambda n : io.recv(n)<cr>rl      = lambda s : io.recvline(s)<cr>#ru = lambda s : io.recvuntil(s)<cr>ru      = lambda delim,drop=True : io.recvuntil(delim, drop)<cr>uu32    = lambda data            : u32(data.ljust(4,b'\x00'))<cr>uu64    = lambda data            : u64(data.ljust(8,b'\x00'))<cr>lg      = lambda s,addr          : io.success('\033[1;31;40m%20s-->0x%x\033[0m'%(s,addr))<cr><cr><cr><cr><cr><cr>io.interactive()<esc>

inoremap <c-f> elf = context.binary = ELF('./<++>')<cr>libc = elf.libc<cr><cr>gs = '''<cr>c<cr>'''<cr>def start():<cr>if args.GDB:<cr>return gdb.debug(elf.path,gdbscript=gs)<cr>elif args.REM:<cr>return remote("<++>",<++>)<cr>else:<cr>return process(elf.path)<esc>




inoremap <c-g> local = 1<cr>elf = ELF('./<++>')<cr>if local:<cr>p = process('./<++>')<cr>#libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')<cr>else:<cr>p = remote('<++>',<++>)<cr>#libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')<esc>


"pwn in vim 
nmap <Leader>gn :!./% GDB NOASLR<CR>
nmap <Leader>gg :!./% GDB<CR>
nmap <Leader>gm :!./% REM<CR>
nmap <Leader>gp :!./%<CR>

nmap <silent> sd :!gcc -g -fno-stack-protector -no-pie -z execstack % -o %<;gdb %<<CR>
nmap <silent> sp :!gcc -g -fno-stack-protector -fPIE -pie -z noexecstack % -o %<<CR>
nmap <silent> sc :!gcc -g -fstack-protector-all -no-pie % -o %<<cr>
nmap <silent> s<CR> :!gdb %<<CR>


"auto center in some symbols
imap '3' ''<right>,''<right>,''<left><left><left><left><left><left>
imap '2' ''<right>,''<left><left><left>
imap "3" ""<right>,""<right>,""<left><left><left><left><left><left>
imap "2" ""<right>,""<left><left><left>
imap {} {}<left>
imap () ()<left>
imap <> <><left>
imap '' ''<left>
imap "" ""<left>
imap [] []<left>



"======autostart========
if empty(glob('~/.vim/autoload/plug.vim'))
    silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs 
        \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
        autocmd VimEnter * PlugInstall --syn | source $MYVIMRC
endif


"======vimplug=========
call plug#begin('~/.vim/autoload')

Plug 'vim-airline/vim-airline'
"Plug 'prabirshre'stha/vim-lsp
"Plug 'Yggdroot/LeaderF', { 'do': './install.sh' }
"Plug 'neoclide/coc.nvim'
"Plug 'skywind3000/asyncrun.vim'
"Plug 'puremourning/vimspector',{'do':'./install_gadget.py --enable-c --enable-python --enable-bash'}
"Plug 'junegunn/vim-easy-align'
Plug 'junegunn/fzf.vim'

Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'

Plug 'tpope/vim-surround'
Plug 'gcmt/wildfire.vim'

Plug 'preservim/nerdtree'

call plug#end()

"======coc.nvim=========
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

function! s:check_back_space() abort
      let col = col('.') - 1
        return !col || getline('.')[col - 1]  =~# '\s'
    endfunction

"======asyncrun=========
noremap <silent><f7> :copen<CR><C-w><C-w>:AsyncRun gcc -g % -o %<<CR>
"noremap <silent><f5> :AsyncRun! ./%<<CR>
"noremap <silent><f2> :AsyncStop!<CR>


" === vimspector
"let g:vimspector_enable_mappings = 'HUMAN'
"noremap <leader>vs :tabe .vimspector.json<CR>:LoadVimSpectorJsonTemplate<CR>
"sign define vimspectorBP text=X  texthl=Normal 
"sign define vimspectorBPDisabled text=* texthl=Normal
"sign define vimspectorPC text=👉 texthl=SpellBad


let g:vimspector_enable_mappings = 'HUMAN'
function! s:read_template_into_buffer(template)
	" has to be a function to avoid the extra space fzf#run insers otherwise
	execute '0r ~/.config/vim/sample_vimspector_json/'.a:template
endfunction
command! -bang -nargs=* LoadVimSpectorJsonTemplate call fzf#run({
			\   'source': 'ls -1 ~/.config/vim/sample_vimspector_json',
			\   'down': 20,
			\   'sink': function('<sid>read_template_into_buffer')
			\ })
noremap <leader>vs :tabe .vimspector.json<CR>:LoadVimSpectorJsonTemplate<CR>
sign define vimspectorBP text=☛ texthl=Normal
sign define vimspectorBPDisabled text=☞ texthl=Normal
sign define vimspectorPC text=☉ texthl=SpellBad

"======easy-align=======
"xmap ga <Plug>(EasyAlign)
"nmap ga <Plug>(EasyAlign)


"======fzf.vim=========
set rtp+=/home/c4se/fzf
"set rtp+=/home/linuxbrew/.linuxbrew/opt/fzf
set rtp+=/home/c4se/.fzf
noremap <silent> <C-p> :Files<CR>
"noremap <silent> <C-p> :Leaderf file<CR>
noremap <silent> <C-f> :Rg<CR>
noremap <silent> <C-h> :History<CR>
"noremap <C-t> :BTags<CR>
noremap <silent> <C-l> :Lines<CR>
noremap <silent> <C-b> :Buffers<CR>
noremap <leader>; :History:<CR>

let g:fzf_preview_window = 'right:60%'
let g:fzf_commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'

function! s:list_buffers()
  redir => list
  silent ls
  redir END
  return split(list, "\n")
endfunction

function! s:delete_buffers(lines)
  execute 'bwipeout' join(map(a:lines, {_, line -> split(line)[0]}))
endfunction

command! BD call fzf#run(fzf#wrap({
  \ 'source': s:list_buffers(),
  \ 'sink*': { lines -> s:delete_buffers(lines) },
  \ 'options': '--multi --reverse --bind ctrl-a:select-all+accept'
\ }))

noremap <c-d> :BD<CR>

let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.8 } }

"======ultisnips=========
let g:UltiSnipsExpandTrigger="<c-a>"
let g:UltiSnipsJumpForwardTrigger="<c-k>"
let g:UltiSnipsJumpBackwardTrigger="<c-i>"
let g:UltiSnipsEditSplit="vertical"
let g:UltiSnipsSnippetDirectories=[$HOME.'/.vim/Ultisnips/','Ultisnips'] 


"======wildfire.vim=======
"<enter>


"======vim-surround=======
" cs'"  ''-->""
" cs"'  ""-->''
" cs")  ""-->)
" cs"]  ""-->]
" cs"}  ""-->}

"========nerdtree========
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'
nnoremap <C-n> :NERDTree<CR>
"nnoremap <C-t> :NERDTreeToggle<CR>
""nnoremap <C-f> :NERDTreeFind<CR>







